package com.usst.handle;


import org.springframework.stereotype.Controller;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.mysql.jdbc.StringUtils;
import com.usst.entity.Post;
import com.usst.entity.PostAnswer;
import com.usst.service.PostAnswerService;
import com.usst.service.PostService;


@Controller
public class PostAnswerHandler extends DefaultHandler {

	private PostService postService;
	
	private PostAnswerService postAnswerService;
	
	private PostAnswer post;
	
	public PostAnswerHandler() {
		super();
	}
	
	public PostAnswerHandler(PostService postService, PostAnswerService postAnswerService) {
		this();
		this.postService = postService;
		this.postAnswerService = postAnswerService;
	}
    
    @Override
    public void startDocument() throws SAXException {
    	
    }
    
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
    
    }
    
	@Override
    public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
        if ("row".equals(name)) {
        	
            post= new PostAnswer();
            
            post.setPostId(Long.valueOf(attributes.getValue("Id")));
            post.setPostTypeId(Long.valueOf(attributes.getValue("PostTypeId")));
            
            if (!StringUtils.isNullOrEmpty(attributes.getValue("ParentId"))) {
            	post.setParentId(Long.valueOf(attributes.getValue("ParentId")));
            }
            
            if (!StringUtils.isNullOrEmpty(attributes.getValue("AcceptedAnswerId"))) {
            	post.setAcceptedAnswerId(Long.valueOf(attributes.getValue("AcceptedAnswerId")));
            }
            
            if (!StringUtils.isNullOrEmpty(attributes.getValue("CreationDate"))) {
            	post.setCreationDate(attributes.getValue("CreationDate")); 
            }
            
            if (!StringUtils.isNullOrEmpty(attributes.getValue("Score"))) {
            	post.setScore(Long.valueOf(attributes.getValue("Score")));
            }
            
            if (!StringUtils.isNullOrEmpty(attributes.getValue("ViewCount"))) {
            	post.setViewCount(Long.valueOf(attributes.getValue("ViewCount")));
            }
            
            if (!StringUtils.isNullOrEmpty(attributes.getValue("Body"))) {
            	post.setBody(attributes.getValue("Body"));
            }
            
            if (!StringUtils.isNullOrEmpty(attributes.getValue("OwnerUserId"))) {
            	post.setOwnerUserId(Long.valueOf(attributes.getValue("OwnerUserId")));
            }
            
            if (!StringUtils.isNullOrEmpty(attributes.getValue("LastEditorUserId"))) {
            	post.setLastEditorUserId(Long.valueOf(attributes.getValue("LastEditorUserId")));
            }
            
            if (!StringUtils.isNullOrEmpty(attributes.getValue("LastEditorDisplayName"))) {
            	post.setLastEditorDisplayName(attributes.getValue("LastEditorDisplayName"));
            }
            
            if (!StringUtils.isNullOrEmpty(attributes.getValue("LastEditDate"))) {
            	post.setLastEditDate(attributes.getValue("LastEditDate"));
            }
            
            if (!StringUtils.isNullOrEmpty(attributes.getValue("LastActivityDate"))) {
            	post.setLastActivityDate(attributes.getValue("LastActivityDate"));
            }
             
            if (!StringUtils.isNullOrEmpty(attributes.getValue("Title"))) {
            	post.setTitle(attributes.getValue("Title"));
            }
            
            if (!StringUtils.isNullOrEmpty(attributes.getValue("Tags"))) {
            	post.setTags(attributes.getValue("Tags"));
            }
            
            if (!StringUtils.isNullOrEmpty(attributes.getValue("AnswerCount"))) {
            	post.setAnswerCount(attributes.getValue("AnswerCount"));
            }
            
            if (!StringUtils.isNullOrEmpty(attributes.getValue("CommentCount"))) {
            	post.setCommentCount(Long.valueOf(attributes.getValue("CommentCount")));
            }
        	
            if (!StringUtils.isNullOrEmpty(attributes.getValue("FavoriteCount"))) {
            	post.setFavoriteCount(Long.valueOf(attributes.getValue("FavoriteCount")));
            }
        	
            if (!StringUtils.isNullOrEmpty(attributes.getValue("CommunityOwnedDate"))) {
            	post.setCommunityOwnedDate(attributes.getValue("CommunityOwnedDate"));
            }
        	  
        }
    }
    
    @Override
    public void endElement(String uri, String localName, String name) throws SAXException {
        if (post != null && "row".equals(name)) {
        	if (post.getPostTypeId() == 2) {
        		Post postRequest = postService.findOne(post.getParentId());
        		if (postRequest != null) {
        			postAnswerService.save(post);
        		}
        		postRequest = null;
        	}
            post = null;
        }
    }
    
    
    
}
